package com.studentexam.student.controller;

import com.studentexam.student.model.Person;
import com.studentexam.student.service.PersonService;
import com.studentexam.student.tool.ResponseMessage;
import com.studentexam.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin
@Api(tags="person")
@RestController
public class PersonController {
    @Autowired
    private PersonService personService;


    @ApiOperation(value = "验证方法")
    @RequestMapping(value = "/selectPerson", method = RequestMethod.GET)
    public ResponseMessage selectBy(String account, String password){
        int isSelect=0;
        isSelect=personService.selectPerson(account,password);
        if (isSelect==1){
            return Result.success("验证成功");
        }else return Result.error("验证失败");
    }


    @ApiOperation(value = "人员添加方法")
    @RequestMapping(value = "/addPerson", method = RequestMethod.GET)
    public ResponseMessage add(Person person) {
        int isAdd = 0;
//        1.判断是否空对象
        if (person != null) {
//        2.判断是否空字段
            if (person.getAccount()!= null) {
//               3.添加数据
                isAdd = personService.addPerson(person);
                if (isAdd==1){
                    return Result.success("保存成功");
                }else if (isAdd==-1){
                    return Result.success("已重复，不允许添加");
                }else return Result.error("保存失败");
            }
        }
        return Result.error("保存失败");
    }


    @ApiOperation(value = "人员信息删除方法")
    @RequestMapping(value = "/delPerson", method = RequestMethod.GET)
    public ResponseMessage delete(int personid){
        if (personid!=0){
            int isDel=0;
            isDel=personService.delPerson(personid);
            if (isDel>0){
                return Result.success("删除人员信息成功");
            }else return Result.error("删除人员信息失败");
        }else return Result.error("删除失败，学号不能为零");
    }

    @ApiOperation(value = "人员信息更新方法")
    @RequestMapping(value = "/updatePerson")
    public ResponseMessage update(Person person) {
        if (person != null) {
            if (person.getPersonid()!= 0) {
                if (personService.updatePerson(person)> 0) {
                    return Result.success("更新人员信息成功");
                } else return Result.error("更新人员信息失败");
            } else return Result.error("更新人员信息失败，学号不能为0");
        } else return Result.error("更新人员信息失败，此对象不能为空");
    }

    @ApiOperation(value = "根据条件获取人员信息")
    @RequestMapping(value = "/getByPersonid",method = RequestMethod.GET)
    public ResponseMessage getByPersonid(int personid){
        Map<String, List> map=new HashMap<>();
        List<Person> personList=personService.getByPersonid(personid);
        map.put("list",personList);
        return Result.success("查询成功",map);
    }
}
